<?php

namespace App\Admin\Controllers\Content;

use App\Admin\Actions\Tree\ArticleCategoryShow;
use App\Models\Content\ArticleCategory;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Repositories\EloquentRepository;
use Dcat\Admin\Repositories\Repository;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Tree;

class ArticleCategoryController extends AdminController
{

    protected Repository $repository;

    public function __construct()
    {
        $this->repository = new EloquentRepository(new ArticleCategory());
        Admin::translator()->setPath("content");
    }

    public function title()
    {
        return admin_trans_label("article-category");
    }

    //第一种方式
    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description(trans('admin.list'))
            #->body($this->grid())  //另一种管理方式
            ->body(function (Row $row) {
                $row->column(6, $this->tree());
                #$row->column(6, $this->form());
            });
    }


    protected function grid()
    {
        $grid =  Grid::make($this->repository, function (Grid $grid) {
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand(true);
                $filter->like('name')->width(2);
            });

            $grid->column('id')->sortable();
            $grid->column('pid');
            $grid->column('name')->tree(true);
            $grid->column('order')->editable()->orderable();
            $grid->column('display')->switch();
            $grid->column('state')->switch();
            $grid->column('created_at')->width(200);
            $grid->column('updated_at')->width(200)->sortable();

            $grid->disableEditButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableBatchDelete();
            $grid->disableViewButton();
            $grid->disablePagination();
        });
        return $grid;
    }


    protected function detail($id)
    {
        return Show::make($id, new ArticleCategory(), function (Show $show) {
            $show->field('id');
            $show->field('pid');
            $show->field('name');
            $show->field('order');
            $show->field('display');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * @return \Dcat\Admin\Tree
     */
    protected function tree()
    {
        return new Tree($this->repository, function (Tree $tree) {
            $tree->disableCreateButton();
            #$tree->disableQuickCreateButton();
            #$tree->disableEditButton();
            $tree->showQuickCreateButton();
            $tree->showQuickEditButton();
            $tree->maxDepth(3);

            $tree->actions(function (Tree\Actions $actions) {
                if ($actions->getRow()->extension) {
                    $actions->disableDelete();
                }
                $actions->prepend(new ArticleCategoryShow());
            });
            $tree->branch(function ($branch) {
                $payload = "&nbsp;<strong>{$branch['name']}</strong>";
                return $payload;
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ArticleCategory(), function (Form $form) {
            $form->display('id');
            $form->select('pid',"上级分类")->options(ArticleCategory::selectOptions());
            $form->text('name');
            $form->radio('display')->default(1)->options(["0"=>"隐藏","1"=>"显示"]);
            $form->radio('state')->default(1)->options(["0"=>"禁用","1"=>"启用"]);
            $form->text('order');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
